<?php

/**
 * IndexController is the default controller for this application
 */
class TemplatePhotoController extends ControllerBase {
	/**
	 * TemplatePhotoDataService object
	 *
	 * @var TemplatePhotoDataService object
	 */
	public $templatePhotoDataService = null;
	
	/**
	 * TemplateDataService object
	 *
	 * @var TemplateDataService object
	 */
	public $templateDataService = null;
	
	/**
	 * Constructor of This Controller
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'templates_gallery' );
		
		// Init all Data Services
		$this->templatePhotoDataService = new TemplatePhotoDataService ();
		$this->templateDataService = new TemplateDataService ();
		
		$this->smarty->assign ( 'statusList', TemplatePhotoEntity::getStatusList () );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		$templateId = $this->getQuery ( 'templateId', '' );
		if ($templateId == "") {
			$templateId = $this->getPost ( 'templateId', '' );
		}
		
		$templateEntity = $this->templateDataService->getById ( $templateId );
		if (empty ( $templateEntity )) {
			// Redirect to Template List Page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Template';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$arrElement = array ();
		$arrElement ['templateId'] = $templateId;
		$this->smarty->assign ( 'itemList', $this->templatePhotoDataService->getItems ( $arrElement, 'createdDate DESC' ) );
		
		// Assign template Name
		$this->smarty->assign ( 'templateName', $templateEntity->name );
		$this->smarty->assign ( 'templateId', $templateId );
		$this->smarty->assign ( 'mainPhotoId', $templateEntity->picture );
		
		// Create new token key
		$this->createActionKey ();
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/TemplatePhoto/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Edit the template photo
	 */
	public function editAction() {
		$templateId = $this->getQuery ( 'templateId', '' );
		if ($templateId == "") {
			$templateId = $this->getPost ( 'templateId', '' );
		}
		
		$photoId = $this->getQuery ( 'id', '' );
		if ($photoId == "") {
			$photoId = $this->getPost ( 'id', '' );
		}
		
		$templateEntity = $this->templateDataService->getById ( $templateId );
		if (empty ( $templateEntity )) {
			// Redirect to Template List Page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Template';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$templatePhotoEntity = $this->templatePhotoDataService->getById ( $photoId );
		if (empty ( $templatePhotoEntity )) {
			// Redirect to Template List Page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Template';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			$title = $this->getPost ( 'title', '' );
			$status = $this->getPost ( 'status', '' );
			// Upload picture
			$picture = '';
			try {
				$picture = CommonUtils::uploadPictureEx ( 'picture' );
			} catch ( Exception $ex ) {
				// Ignore this case
				$resultMessage = 'Upload Photo Fail.';
			}
			
			$templatePhotoEntity->title = $title;
			if (! empty ( $picture )) {
				$templatePhotoEntity->picture = $picture;
			}
			$templatePhotoEntity->status = $status;
			$this->templatePhotoDataService->update ( $templatePhotoEntity );
			$resultMessage = 'The record has been saved successfully';
			$this->smarty->assign ( 'resultMessage', $resultMessage );
		}
		
		// Get Value of Template Photo
		$this->smarty->assign ( 'title', $templatePhotoEntity->title );
		$this->smarty->assign ( 'status', $templatePhotoEntity->status );
		$this->smarty->assign ( 'templatePhotoEntity', $templatePhotoEntity );
		
		$this->smarty->assign ( 'templateId', $templateId );
		$this->smarty->assign ( 'id', $photoId );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/TemplatePhoto/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Add new Template Screen shot
	 */
	public function addAction() {
		$templateId = $this->getQuery ( 'templateId', '' );
		if ($templateId == "") {
			$templateId = $this->getPost ( 'templateId', '' );
		}
		
		$templateEntity = $this->templateDataService->getById ( $templateId );
		if (empty ( $templateEntity )) {
			// Redirect to Template List Page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Template';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			$title = $this->getPost ( 'title', '' );
			$status = $this->getPost ( 'status', '' );
			
			// Upload picture
			$picture = '';
			try {
				$picture = CommonUtils::uploadPictureEx ( 'picture' );
			} catch ( Exception $ex ) {
				// Ignore this case
				$resultMessage = 'Upload templatePhoto fail.';
			}
			
			if (! empty ( $picture )) {
				try {
					$templatePhotoEntity = $this->templatePhotoDataService->beforeCreate ();
					$templatePhotoEntity->title = $title;
					$templatePhotoEntity->picture = $picture;
					$templatePhotoEntity->description = "";
					$templatePhotoEntity->url = "";
					$templatePhotoEntity->status = $status;
					$templatePhotoEntity->viewed = 0;
					$templatePhotoEntity->comment = "";
					$templatePhotoEntity->templateId = $templateId;
					$this->templatePhotoDataService->create ( $templatePhotoEntity );
					
					$resultMessage = 'The record has been saved successfully';
				} catch ( Exception $ex ) {
					$resultMessage = 'The record cannot create';
				}
			}
			$this->smarty->assign ( 'resultMessage', $resultMessage );
		}
		
		$this->smarty->assign ( 'templateId', $templateId );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/TemplatePhoto/add.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Set status to inactive
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function inactiveAction() {
		$this->updateStatus ( TemplatePhotoEntity::INACTIVE );
		
		// View inactive list
		$this->indexAction ();
	}
	
	/**
	 * Set status to active
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function activeAction() {
		$this->updateStatus ( TemplatePhotoEntity::ACTIVE );
		
		// View templatePhoto list
		$this->indexAction ();
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a templatePhoto
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		if ($this->validateActionKey ()) {
			
			// Get Id of templatePhoto
			$id = $this->getPost ( 'idValue', '' );
			
			// Delete a templatePhoto item
			$res = $this->templatePhotoDataService->del ( $id );
		}
		
		// View templatePhoto list
		$this->indexAction ();
	}
	
	/**
	 * Delete list of templatePhoto
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteallAction() {
		if ($this->validateActionKey ()) {
			$iCount = $this->getPost ( 'iCount', 0 );
			for($i = 0; $i < $iCount; $i ++) {
				$id = $this->getPost ( 'checkItem' . $i, '' );
				if (! empty ( $id )) {
					
					// Delete a templatePhoto item
					$this->templatePhotoDataService->del ( $id );
				}
			}
		}
		
		// View templatePhoto list
		$this->indexAction ();
	}
	
	/**
	 * Update status list of templatePhoto
	 *
	 * @access private
	 *
	 * @param int $status: Status of the templatePhoto
	 *
	 * @return void
	 */
	protected function updateStatus($status) {
		if ($this->validateActionKey ()) {
			$iCount = $this->getPost ( 'iCount', 0 );
			for($i = 0; $i < $iCount; $i ++) {
				$id = $this->getPost ( 'checkItem' . $i, '' );
				if (! empty ( $id )) {
					$this->templatePhotoDataService->updateStatus ( $id, $status );
				}
			}
		}
	}
	
	/**
	 * Set Photo as Main Thumbnail
	 */
	public function setMainAction() {
		$templateId = $this->getQuery ( 'templateId', '' );
		$photoId = $this->getQuery ( 'id', '' );
		
		$templateEntity = $this->templateDataService->getById ( $templateId );
		if (empty ( $templateEntity )) {
			// Redirect to Template List Page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Template';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$templatePhotoEntity = $this->templatePhotoDataService->getById ( $photoId );
		if (empty ( $templatePhotoEntity )) {
			// Redirect to Template List Page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Template';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		// Update Main Photo
		$templateEntity->picture = $photoId;
		$this->templateDataService->update ( $templateEntity );
		
		// Redirect to Template List Page
		$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/TemplatePhoto/Index/templateId/' . $templateId;
		CommonUtils::redirect ( $url );
	
	}
}
